package com.zsyh.jiujiu.dialog;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.adepter.CommonAdapter;
import com.zsyh.jiujiu.adepter.shengjiAdepter;
import com.zsyh.jiujiu.bean.DizhiBean;
import com.zsyh.jiujiu.net.MyFragementObserver;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import razerdp.basepopup.BasePopupWindow;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DiquPop extends BasePopupWindow {

    private TextView sheng,chengshi;
    private RecyclerView shenglist,citylist;
    private shengjiAdepter shengjiAdepter;
    List<DizhiBean.DataBean> list = new ArrayList<>();
    List<Integer> listone = new ArrayList<>();

    private shengjiAdepter cityAdepter;
    List<DizhiBean.DataBean> listcity = new ArrayList<>();
    List<Integer> listtwo = new ArrayList<>();
    private OnSureClickListener onSureClickListener;
    private FragmentActivity mContext;
    private View viewtone,viewtwo;
    Context context;
    public DiquPop(Context context, FragmentActivity mContext){
        super(context);
        this.mContext=mContext;
        this.context=context;
        setPopupGravity(Gravity.RIGHT);
        sheng=findViewById(R.id.sheng);
        chengshi=findViewById(R.id.chengshi);
        shenglist=findViewById(R.id.shenglist);
        citylist=findViewById(R.id.citylist);
        viewtone=findViewById(R.id.viewone);
        viewtwo=findViewById(R.id.viewtwo);
        viewtone.setBackgroundColor(context.getResources().getColor(R.color.diziselect));
        viewtwo.setBackgroundColor(context.getResources().getColor(R.color.white));
        sheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shenglist.setVisibility(View.VISIBLE);
                citylist.setVisibility(View.GONE);
                viewtone.setBackgroundColor(context.getResources().getColor(R.color.diziselect));
                viewtwo.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
        });

        chengshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shenglist.setVisibility(View.GONE);
                citylist.setVisibility(View.VISIBLE);
                viewtone.setBackgroundColor(context.getResources().getColor(R.color.white));
                viewtwo.setBackgroundColor(context.getResources().getColor(R.color.diziselect));
            }
        });


        sheng("0");


    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.dialogdiqu);
    }

    /**
     * 省
     */
    public void sheng(String id){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(mContext,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);
                        if(list!=null){
                            list.clear();
                        }
                        for(int i=0;i<dizhiBean.getData().size();i++){
                            list.add(dizhiBean.getData().get(i));
                            listone.add(0);
                        }
                        shengjiAdepter=new shengjiAdepter(context,list,listone);
                        shenglist.setLayoutManager(new LinearLayoutManager(context));
                        shenglist.setAdapter(shengjiAdepter);
                        shengjiAdepter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                setpostion(listone,position);
                                shengjiAdepter.notifyDataSetChanged();
                                shenglist.setVisibility(View.GONE);
                                citylist.setVisibility(View.VISIBLE);
                                chengshi.setText(list.get(position).getAreaName());
                                chengshi.setText("请选择");
                                viewtone.setBackgroundColor(context.getResources().getColor(R.color.diziselect));
                                viewtwo.setBackgroundColor(context.getResources().getColor(R.color.white));
                                getchengshi(list.get(position).getId());
                            }
                        });
                    }
                });
    }

    public void setpostion(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }

    public void setpostiontwo(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }

    /**
     * 城市
     */
    public void getchengshi(String id){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(mContext,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);
                        if(listcity!=null){
                            listcity.clear();
                        }
                        for(int i=0;i<dizhiBean.getData().size();i++){
                            listcity.add(dizhiBean.getData().get(i));
                            listtwo.add(0);
                        }
                        cityAdepter=new shengjiAdepter(context,listcity,listtwo);
                        citylist.setLayoutManager(new LinearLayoutManager(context));
                        citylist.setAdapter(cityAdepter);
                        cityAdepter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                setpostion(listtwo,position);
                                cityAdepter.notifyDataSetChanged();
                                chengshi.setText(listcity.get(position).getAreaName());
                                viewtone.setBackgroundColor(context.getResources().getColor(R.color.white));
                                viewtwo.setBackgroundColor(context.getResources().getColor(R.color.diziselect));
                                onSureClickListener.callback(listcity.get(position).getId(),listcity.get(position).getAreaName());
                                dismiss();
                            }
                        });
                    }
                });
    }

    public void setOnSureClickListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    public interface OnSureClickListener {

        void callback(String cityid,String name);

    }

}
